package saobracaj;

import java.util.ArrayList;

public class Semafor {

	private short svetlo;
	private char tip;
	private boolean crvzut = false;
	public int taster;
	public boolean prethodniTaster;
	private int brRask;
	
	private boolean treptanje = false;
	
	private ArrayList<Pesak> tmp1 = new ArrayList<Pesak>();
	private ArrayList<Pesak> tmp2 = new ArrayList<Pesak>();
	private ArrayList<Automobil> tmp = new ArrayList<Automobil>();
	
	private Object sync;
	private Raskrsnica rask;
	// private boolean flag;

	public Semafor(Raskrsnica r, char type, int br) {
		rask = r;
		svetlo = 0;
		prethodniTaster = false;
		brRask = br;
		taster = -1;
		if (type == 'b')
			taster = 0;
		this.tip = type;
		
		// flag = false;
	}
	
	public void initSemafor() {
		switch (brRask) {
		case 1:
			tmp1 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPsever1();
			tmp2 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPsever2();
			break;
		case 2:
			tmp1 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPistok1();
			tmp2 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPistok2();
			break;
		case 3:
			tmp1 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPjug1();
			tmp2 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPjug2();
			break;
		case 4:
			tmp1 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPzapad1();
			tmp2 = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getPzapad2();
			break;
		case 11:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getSeverD();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncSever();
			break;
		case 12:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getSeverP();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncSever();
			break;
		case 13:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getSeverL();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncSever();
			break;
		case 21:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getIstokD();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncIstok();
			break;
		case 22:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getIstokP();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncIstok();
			break;
		case 23:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getIstokL();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncIstok();
			break;
		case 31:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getJugD();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getSyncJug();
			break;
		case 32:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getJugP();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getSyncJug();
			break;
		case 33:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getJugL();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getSyncJug();
			break;
		case 41:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getZapadD();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncZapad();
			break;
		case 42:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getZapadP();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncZapad();
			break;
		case 43:
			tmp = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask().getZapadL();
			sync = RaskrsnicaGUI.dohvatiRaskrsnicu().dohvatiRask()
					.getSyncZapad();
			break;
		}
	}

	public void pritisniTaster() {
		taster = 1;
	}

	public void postaviZeleno() {
		
			if (taster != 0 || !prethodniTaster) {
				svetlo = 3;
				
				if (taster == 0 && !prethodniTaster)
					prethodniTaster = true;
				if (taster == 1) {
					prethodniTaster = true;
					taster = 0;
				}
				
				
				if (tip == 'b') {
					for (int i = 0; i < tmp1.size(); i++)
						tmp1.get(i).prelazak();
					for (int i = 0; i < tmp2.size(); i++)
						tmp2.get(i).prelazak();
				}
				System.out.println("svetlo je "+svetlo);
				
				
			} else
				prethodniTaster = false;
			
			crvzut = false;		
	}
	
	public void javi() {
		if (tip == 'a')
			synchronized (sync) { 
				System.out.println("size"+tmp.size());
				/*for(Automobil auto: tmp) {
					System.out.println("id je " + auto.getId());
					auto.prelazak();	
					//tmp.remove(auto);
				}
				tmp.clear();	*/	
				while (tmp.size()!=0) {
					tmp.get(0).prelazak();
					tmp.remove(0);
				}
			}
		//System.out.println("dsds");	
	}

	public void postaviZeleno(int i) {
		svetlo = 3;
		crvzut = false;
	}

	public void postaviZuto() {
		svetlo = 2;
	}

	public void postaviCrveno() {
		svetlo = 1;
		switch(brRask){
			case 11: rask.severDsenz = false;
					 break;
			case 12: rask.severPsenz = false;
					 break;
			case 13: rask.severLsenz = false;
				     break;
			case 21: rask.istokDsenz = false;
					 break;
			case 22: rask.istokPsenz = false;
					 break;
			case 23: rask.istokLsenz = false;
					 break;
			case 31: rask.jugDsenz = false;
					 break;
			case 32: rask.jugPsenz = false;
					 break;
			case 33: rask.jugLsenz = false;
					 break;
			case 41: rask.zapadDsenz = false;
					 break;
			case 42: rask.zapadPsenz = false;
					 break;
			case 43: rask.zapadLsenz = false;
					 break;
	
		}
	}

	public void postaviIskljuceno() {
		svetlo = 0;
	}

	public void postaviCrvenoZuto() {
		crvzut = true;
	}

	public short dohvatiSvetlo() {
		return svetlo;
	}

	public void postaviSvetlo(short colour) {
		this.svetlo = colour;
	}

	public boolean crvenoZuto() {
		return crvzut;
	}

	public boolean isTreptanje() {
		return treptanje;
	}

	public void setTreptanje(boolean treptanje) {
		this.treptanje = treptanje;
	}

}
